package top.blackr.folivora.common.security.crud.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.blackr.folivora.common.core.R;
import top.blackr.folivora.common.log.annotation.SysLog;
import top.blackr.folivora.common.security.crud.model.entity.SysRole;
import top.blackr.folivora.common.security.crud.model.vo.SysRoleVo;
import top.blackr.folivora.common.security.crud.repository.SysRoleRepository;

@Tag(name = "角色")
@RestController
@AllArgsConstructor
@RequestMapping("role")
public class SysRoleController {

    private final SysRoleRepository roleService;

    @GetMapping("page")
    @SaCheckPermission("role_read")
    @Operation(summary = "分页信息", description = "分页列表")
    public R<Page<SysRoleVo>> getRolePage(Page<SysRole> page, SysRoleVo params) {
        return R.ok(roleService.roleInfoPage(page, params));
    }

    @SysLog("'新增角色 ' + #params.roleName")
    @PostMapping("save")
    @Operation(summary = "新增角色")
    @SaCheckPermission("role_write")
    public R<Boolean> saveRole(@RequestBody SysRoleVo params) {
        return R.ok(roleService.saveRole(params));
    }

    @SysLog("'更新角色 ' + #params.roleId")
    @PostMapping("update")
    @Operation(summary = "更新角色")
    @SaCheckPermission("role_write")
    public R<Boolean> updateRole(@RequestBody SysRoleVo params) {
        return R.ok(roleService.updateRole(params));
    }

    @SysLog("'删除角色 ' + #params.roleId")
    @PostMapping("delete")
    @Operation(summary = "删除角色")
    @SaCheckPermission("role_del")
    public R<Boolean> delRole(@RequestBody SysRoleVo params) {
        return R.ok(roleService.delRole(params));
    }

    @PostMapping("relationMenus")
    @Operation(summary = "角色关联菜单（包含父级id）")
    @SaCheckPermission("role_relation_menu")
    public R<Boolean> relationMenus(@RequestBody SysRoleVo params) {return R.ok(roleService.relationMenus(params)); }
}
